#!/bin/sh
## java env
export JAVA_HOME=/chentongwei/dev-tools/jdk1.8.0_121
export JRE_HOME=$JAVA_HOME/jre

## The script directory, and the packages that need to be deployed are also placed in this directory
SCRIPT_DIR=/chentongwei/autoDeploy/scripts

## Old package backup directory
BAK_DIR=/chentongwei/bak/clinic-web
## Date format for old packet backup,such as [xxx.jar-V1.1.1-2018-02-11-11:11:11]
BAK_DATE_FORMAT=`date '+%Y-%m-%d-%H:%M:%S'`

## Project deployment directory,such as tomcat's webapps
TOMCAT_NAME=tomcat-8.0.43_clinic
TOMCAT_DIR=/chentongwei/project/$TOMCAT_NAME
SERVICE_DIR=$TOMCAT_DIR/webapps
## Project name.
SERVICE_NAME=clinic-web
## Project full name
JAR_NAME=$SERVICE_NAME\.war
## To kill the process and view the state of the process
PID=$SERVICE_NAME\.pid


start() {
    sh $TOMCAT_DIR/bin/startup.sh 
    echo `ps -ef|grep $TOMCAT_NAME/bin/bootstrap.jar | grep java | awk '{print $2}'` > $SERVICE_DIR/$PID
    echo "====== starting $SERVICE_NAME ======="
} 

stop() {  
    kill `cat $SERVICE_DIR/$PID`
    rm -rf $SERVICE_DIR/$PID
    echo "=== stop $SERVICE_NAME"

    sleep 5
	## -w all match，very important
	## edu-service-aa.jar
	## edu-service-aa-bb.jar
    P_ID=`ps -ef|grep $TOMCAT_NAME/bin/bootstrap.jar | grep java | awk '{print $2}'`
    if [ "$P_ID" == "" ]; then
        echo "=== $SERVICE_NAME process not exists or stop success"
    else
        echo "=== $SERVICE_NAME process pid is:$P_ID"
        echo "=== begin kill $SERVICE_NAME process, pid is:$P_ID"
        kill -9 $P_ID
    fi
} 

restart() {  
    echo "stoping ... "  
    stop  
    sleep 2
    echo "staring ..."  
    start  
}  

status() {
    P_ID=`ps -ef|grep $TOMCAT_NAME/bin/bootstrap.jar | grep java | awk '{print $2}'`
    if [ "$P_ID" == "" ]; then
 	echo "======stoped======"
    else 
	echo "======starting======"
    fi
}

copyasbak() {
    # check param
    VERSION="$1"
    if [ "$VERSION" == "" ]; then 
        echo "=============================================================================================================="
        echo "The version number can not null and  must be larger than six bits and must be the beginning of the V. [V1.0.0]" 
        echo "=============================================================================================================="
        exit 0
    fi
    VERSION_LENGTH=${#VERSION}
    if [ ${#VERSION} -lt '6' -o ${VERSION:0:1} != 'V' ]; then
        echo "============================================================================================"
        echo "The version number must be larger than six bits and must be the beginning of the V. [V1.0.0]" 
        echo "============================================================================================"
	exit 0
    fi
    # copy and bak
    echo "start copy......please wait......"
    # copy as bak and rename
    if [ -f "$SERVICE_DIR"/"$JAR_NAME" ]; then
        mv $SERVICE_DIR/$JAR_NAME $BAK_DIR/$JAR_NAME.$1.$BAK_DATE_FORMAT
    fi
    # delete webapps programe folder        
    rm -rf $SERVICE_DIR/$SERVICE_NAME
    # new project move to webapps
    if [ -f "$SCRIPT_DIR"/"$JAR_NAME" ]; then
        mv $SCRIPT_DIR/$JAR_NAME /$SERVICE_DIR
    fi
    echo "copy done............"
}

if [ $# -gt 0 ]; then
    if [ "$1" == "start" ]; then
    	start
    elif [ "$1" == "stop" ]; then
        stop
    elif [ "$1" == "restart" ]; then
    	restart
    elif [ "$1" == "status" ]; then
        status
    elif [ "$1" == "start2" ]; then	
	copyasbak $2	
	start
    elif [ "$1" == "restart2" ]; then
	copyasbak $2
	restart
    else
        echo "================================================================================================="
        echo "The parameter is illegal, the first parameter must be [start|stop|restart|status|start2|restart2]"
	echo "================================================================================================="
    fi
else
    echo "==================================================================================================================================================="
    echo "The parameters must be two, and the first is the command [start|stop|restart|status|start2|restart2], and the second is the version number [V1.0.1]"
    echo "==================================================================================================================================================="
fi

exit 0
